﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Owin.Hosting;
using SAM.Framework.WebServer;

namespace SAM.Framework.Owin.NET40
{
    public class WebServerForOwin : IWebServer
    {
        private IDisposable instance;
        public void Dispose()
        {
            if (instance != null)
            {
                instance.Dispose();
            }
        }

        public void Start(WebServerConfig config)
        {
            foreach (var assemblyName in config.Assemblies)
            {
                string fullPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, assemblyName);
                if (System.IO.File.Exists(fullPath))
                {
                    Assembly assembly = Assembly.LoadFile(fullPath);
                    AppDomain.CurrentDomain.Load(assembly.GetName());
                }
            }
            string url = string.Format("http://*:{0}", config.Port);
            instance = WebApp.Start<StartUp>(url);
        }
    }
}
